using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UiInGame : MonoBehaviour
{
    private Player player;
    private Slider slider;
    private Image dashCooldownImage;
    private Image parryImage;
    private Image crystalImage;
    private Image swordImage;
    private Image blackholeImage;
    private Image flaskImage;
    [Header("Souls info")]
    private TextMeshProUGUI currentSouls;
    [SerializeField] private float soulsAmount;
    [SerializeField] private float increaseRate = 100f;

    private void Awake()
    {
        player = FindAnyObjectByType<Player>();
        slider = transform.Find("HpUi").GetComponent<Slider>();
        dashCooldownImage = GameObject.Find("DashCooldown").GetComponent<Image>();
        parryImage = GameObject.Find("ParryCooldown").GetComponent<Image>();
        crystalImage = GameObject.Find("CrystalCooldown").GetComponent<Image>();
        swordImage = GameObject.Find("SwordCooldown").GetComponent<Image>();
        blackholeImage = GameObject.Find("BlackholeCooldown").GetComponent<Image>();
        flaskImage = GameObject.Find("FlaskCooldown").GetComponent<Image>();
        currentSouls = transform.Find("CurrencyUi").GetComponentInChildren<TextMeshProUGUI>();
    }

    private void Start()
    {
        if (player && player.Stats)
            player.Stats.OnHpChanged += UpdateHpUi;
    }

    private void Update()
    {
        UpdateSoulsUi();
        if (Input.GetKeyDown(KeyCode.LeftShift) && player.SkillManager.Dash.DashUnlocked)
            SetCooldownOf(dashCooldownImage);
        if (Input.GetKeyDown(KeyCode.Q) && player.SkillManager.Parry.ParryUnlocked)
            SetCooldownOf(parryImage);
        if (Input.GetKeyDown(KeyCode.F) && player.SkillManager.Crystal.CrystalUnlocked)
            SetCooldownOf(crystalImage);
        if (Input.GetKeyDown(KeyCode.Mouse1) && player.SkillManager.Sword.SwordUnlocked)
            SetCooldownOf(swordImage);
        if (Input.GetKeyDown(KeyCode.R) && player.SkillManager.Blackhole.BlackholeUnlocked)
            SetCooldownOf(blackholeImage);
        if (Input.GetKeyDown(KeyCode.Alpha1) && player.Inventory.GetEquipment(EquipmentType.Flask))
            SetCooldownOf(flaskImage);
        CheckCooldownOf(dashCooldownImage, player.SkillManager.Dash.cooldownTime);
        CheckCooldownOf(parryImage, player.SkillManager.Parry.cooldownTime);
        CheckCooldownOf(crystalImage, player.SkillManager.Crystal.cooldownTime);
        CheckCooldownOf(swordImage, player.SkillManager.Sword.cooldownTime);
        CheckCooldownOf(blackholeImage, player.SkillManager.Blackhole.cooldownTime);
        CheckCooldownOf(flaskImage, player.Inventory.FlaskCooldown);
    }

    private void UpdateSoulsUi()
    {
        if (soulsAmount < PlayerManager.Instance.currency)
        {
            soulsAmount += Time.deltaTime * increaseRate;
        }
        else
        {
            soulsAmount = PlayerManager.Instance.currency;
        }
        currentSouls.text = ((int)soulsAmount).ToString();
    }

    private void UpdateHpUi(int maxHp, int currentHp)
    {
        slider.maxValue = maxHp;
        slider.value = currentHp;
    }

    private void SetCooldownOf(Image image)
    {
        if (image.fillAmount <= 0)
            image.fillAmount = 1;
    }

    private void CheckCooldownOf(Image image, float cooldown)
    {
        if (image.fillAmount > 0)
            image.fillAmount -= 1 / cooldown * Time.deltaTime;
    }
}